﻿using System;
using TS.Entities;
using TS.DataAccess;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

namespace TS.Bussiness
{
    public class GatewayBussiness
    {
        /// <summary>
        /// Select Gateway Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<Gateway> ReadGatewayPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
        {
            totalRecord = 0;
            var dataAccess = new GatewayDataAccess();
            try
            {
                if (pageIndex >= 0 && pageSize > 0)
                {
                    var result = dataAccess.ReadGateWayPaged(pageSize, pageIndex, strFilter, strOrderBy, out totalRecord);
                    if (result != null)
                    {
                        return result;
                    }
                    return null;
                }
                return null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }


        /// <summary>
        /// Insert Gateway
        /// </summary>
        /// <param name="gateway"></param>
        /// <returns></returns>
        public int InsertGateway(Gateway gateway)
        {
            if (string.IsNullOrEmpty(gateway.Name)) return -1;
            GatewayDataAccess gatewayDao = new GatewayDataAccess();
            try
            {
                return gatewayDao.InsertGateway((gateway));
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }


        /// <summary>
        /// Insert Gateway
        /// </summary>
        /// <param name="gateway"></param>
        /// <returns></returns>
        public int DeleteGateway(Gateway gateway)
        {
            if (gateway.GatewayId <= 0) return -1;
            GatewayDataAccess gatewayDao = new GatewayDataAccess();
            try
            {
                return gatewayDao.DeleteGateway(gateway);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        /// <summary>
        /// Insert Gateway
        /// </summary>
        /// <param name="gateway"></param>
        /// <returns></returns>
        public int UpdateGateway(Gateway gateway)
        {
            if (string.IsNullOrEmpty(gateway.Name)) return -1;
            GatewayDataAccess gatewayDao = new GatewayDataAccess();
            try
            {
                return gatewayDao.UpdateGateway(gateway);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public Collection<Gateway> GetAllGateway()
        {
            GatewayDataAccess grpDao = new GatewayDataAccess();
            try
            {
                Collection<Gateway> grlList = new Collection<Gateway>();
                grlList = grpDao.GetAllGateway();
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }


        public Gateway GetGatewayById(int gatewayId)
        {
            GatewayDataAccess grpDao = new GatewayDataAccess();
            try
            {
                Gateway grlList = new Gateway();
                grlList = grpDao.GetByGatewayId(gatewayId);
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }
    }
}
